package com.lq.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lq.common.Mapper.ShortPageMapper;
import com.lq.common.entity.ShortPage;
import com.lq.common.enums.PageNameEnum;
import com.lq.common.param.ResponseParam;
import com.lq.common.param.request.ShortPageReq;
import com.lq.common.service.ShortPageService;
import com.lq.common.until.GetUntile;
import com.lq.common.until.Pages;
import com.lq.common.until.Send;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 权哥
 * @version 1.0
 * @date 2023/7/21 23:01
 */
@Service
public class ShortPageServiceImpl implements ShortPageService {

    @Autowired
    private ShortPageMapper shortPageMapper;

    static GetUntile untile = new GetUntile();
    static Pages pages = new Pages();

    Send send = new Send();
    private static final Logger logger = LoggerFactory.getLogger(Logger.class);

    @Override
    public void insert(PageNameEnum url) throws ParseException, InterruptedException {

        int count=0;

        for (int i = 1; i <= 100; i++) {
            ShortPage insertShortPage = getPage(url);

            if (insertShortPage.getMsg() != null && insertShortPage.getMsg().equals("success")) {
                int row = shortPageMapper.insert(insertShortPage);
                if (row == 1) {
                    count++;
                    logger.info("[" + url.getKeyName() + "]新增成功");
                } else {
                    continue;
                }
            } else {
                break;
            }
            Thread.sleep(5000);
        }

//        send.sendQQEmail("【定时任务】系统提示","["+url.getKeyName()+"]收录成功条数："+count+"。");

    }

    @Override
    public void sendPage(String type) {
        ShortPage shortPage = shortPageMapper.getOne(type);
        send.sendQQEmail("每日【舔狗日记】",shortPage.getContent());
    }

    @Override
    public ResponseParam findList(ShortPageReq req) {

        Page<ShortPage> page = new Page<>(req.getCurrent(), req.getSize());

        QueryWrapper wrapper = new QueryWrapper();

        wrapper.orderByDesc("id");
        ResponseParam responseParam = new ResponseParam();
        responseParam.setData(shortPageMapper.selectPage(page,wrapper));
        return responseParam;
    }

    public static ShortPage getPage(PageNameEnum url) throws ParseException {
        ShortPage shortPage = new ShortPage();

        Map<String, String> resMap = new HashMap<>();
        resMap = pages.getPage(url.toString());

        if (resMap.get("msg").equals("success")) {
            shortPage.setMsg(resMap.get("msg"));
            shortPage.setContent(resMap.get("content"));
            shortPage.setUrl(resMap.get("requestUrl"));
            shortPage.setType(url.getKeyName());
            shortPage.setTime(untile.getDateTime());
        }

        return shortPage;
    }

}
